package system.windows

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class TextBoxWindows(
    context: Context, map: Map<*, *>, close: () -> Unit, front: () -> Unit
) {
    private val textViews: MutableList<TextView> = mutableListOf()
    var view: View = LayoutInflater.from(context).inflate(R.layout.text_box_layout, null)

    init {
        val background = view.findViewById<LinearLayout>(R.id.background_view)
        val closeIcon = view.findViewById<ImageView>(R.id.bt_close)
        closeIcon?.setOnClickListener {
            close()
        }
        val full = view.findViewById<ImageView>(R.id.bt_full)
        full?.setOnClickListener {
            front()
        }
        if (background != null) {
            val num = map["num"] as Int
            val fontSize = map["fontSize"] as Int
            textViews.clear()
            background.removeAllViews()
            for (i in 0 until num) {
                val textView =
                    LayoutInflater.from(context).inflate(R.layout.text_layout, null) as TextView
                textView.textSize = fontSize.toFloat()
                textView.layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    1.0f
                )
                background.addView(textView)
                textViews.add(textView)
            }
        }
    }


    fun onMethodCall(call: MethodCall, result: MethodChannel.Result): Boolean {
        when (call.method) {
            "updateTextBoxText" -> {
                updateText(call.arguments as List<String>)
                result.success(true)
                return true
            }
        }
        return false
    }


    fun clear() {
        textViews.clear()
    }

    private fun updateText(strings: List<String>) {
        Log.d("updateText==", "更新文字框")
        for (i in 0 until strings.count()) {
            textViews[i].text = strings[i]
        }
    }

}